package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.view.swipe.SwipeMenu;
import com.zcx.helper.view.swipe.SwipeMenuListView;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.HistoryAdapter;
import com.zcx.qshop.db.QSDataBase;
import com.zcx.qshop.entity.Good;
import com.zcx.qshop.menu.DeleteMenuCreator;
import com.zcx.qshop.view.TitleView;

/**
 * Created by Administrator on 2015/8/22 0022.
 */
public class HistoryActivity extends QSActivity {

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.history_list_view) private SwipeMenuListView swipeMenuListView;

    @BoundView(R.id.history_delete_all) private View deleteAll;

    private HistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_history);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("浏览历史");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        swipeMenuListView.setMenuCreator(new DeleteMenuCreator(this));

        swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {

            @Override
            public void onMenuItemClick(int i, SwipeMenu swipeMenu, int i1) {

                QSDataBase.HistoryTable.delete(adapter.getItem(i));

                adapter.remove(adapter.getItem(i));

                adapter.notifyDataSetChanged();

            }

        });

        swipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Good good = adapter.getItem(position);

                startVerifyActivity(CommodityDetailActivity.class, new Intent().putExtra("Good", good));

            }

        });

        swipeMenuListView.setAdapter(adapter = new HistoryAdapter(this, QSDataBase.HistoryTable.queryAll(QSApplication.QSPreferences.readSchool().sid)));

        deleteAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                adapter.clear();

                adapter.notifyDataSetChanged();

                QSDataBase.HistoryTable.deleteAll();

            }

        });

    }

}
